package com.example.account.tcc;

import com.example.account.mapper.AccountMapper;
import com.example.account.pojo.Account;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Component
public class AccountTccActionImpl implements AccountTccAction {
    @Autowired
    private AccountMapper accountMapper;

    @Transactional
    @Override
    public boolean prepare(BusinessActionContext context,Long userId,BigDecimal money) {
        Account account = accountMapper.selectByUserId(userId);
        if (account.getResidue().compareTo(money)<0){
            throw new RuntimeException("可用金额不足");
        }
        accountMapper.updateResidueToFrozen(userId, money);
        //添加成功标记
        ResultHolder.setResult(AccountTccAction.class,context.getXid() , "p");
        return true;
    }

    @Transactional
    @Override
    public boolean commit(BusinessActionContext context) {
        if (ResultHolder.getResult(AccountTccAction.class,context.getXid())==null){
            return true;
        }
        Long userId = Long.valueOf(context.getActionContext("userId").toString());
        BigDecimal money = new BigDecimal(context.getActionContext("money").toString());
        accountMapper.updateFrozenToUsed(userId, money);
        //成功删除标记
        ResultHolder.removeResult(AccountTccAction.class,context.getXid());
        return true;
    }

    @Transactional
    @Override
    public boolean rollback(BusinessActionContext context) {
        if (ResultHolder.getResult(AccountTccAction.class,context.getXid())==null){
            return true;
        }
        Long userId = Long.valueOf(context.getActionContext("userId").toString());
        BigDecimal money = new BigDecimal(context.getActionContext("money").toString());
        accountMapper.updateFrozenToResidue(userId, money);
//        accountMapper.deleteById(userId);
        ResultHolder.removeResult(AccountTccAction.class,context.getXid());
        return true;
    }
}
